package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Usecase;
import com.mbyte.easy.admin.service.IUsecaseService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.mapper.SysUserMapper;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.util.PageInfo;
import com.mbyte.easy.util.Utility;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Controller
@RequestMapping("/usecase/usecase")
public class UsecaseController extends BaseController  {

    private String prefix = "usecase/usecase/";

    @Autowired
    private IUsecaseService usecaseService;
    @Autowired
    private IUserService userService;
    @Autowired
    SysUserMapper sysUserMapper;

    /**
    * 查询列表
    *
    * @param model
    * @param pageNo
    * @param pageSize
    * @param usecase
    * @return
    */
    @RequestMapping
    public String index(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String caseCreatetimeSpace, String caseUpdatetimeSpace, Usecase usecase) {
        Page<Usecase> page = new Page<Usecase>(pageNo, pageSize);
        QueryWrapper<Usecase> queryWrapper = new QueryWrapper<Usecase>();
        if(!ObjectUtils.isEmpty(usecase.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",usecase.getEquId());
         }
        if(!ObjectUtils.isEmpty(usecase.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",usecase.getEquName());
         }
        if(!ObjectUtils.isEmpty(usecase.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",usecase.getEquType());
         }
        if(!ObjectUtils.isEmpty(usecase.getEquUser())) {
            queryWrapper = queryWrapper.like("equ_user",usecase.getEquUser());
         }
        if(!ObjectUtils.isEmpty(usecase.getEquStatus())) {
            queryWrapper = queryWrapper.like("equ_status",usecase.getEquStatus());
         }
        if(!ObjectUtils.isEmpty(usecase.getEquStaff())) {
            queryWrapper = queryWrapper.like("equ_staff",usecase.getEquStaff());
         }
        if(!ObjectUtils.isEmpty(usecase.getCaseCreatetime())) {
            queryWrapper = queryWrapper.like("case_createtime",usecase.getCaseCreatetime());
         }
        if(!ObjectUtils.isEmpty(usecase.getCaseSite())) {
            queryWrapper = queryWrapper.like("case_site",usecase.getCaseSite());
         }
        if(!ObjectUtils.isEmpty(usecase.getCaseUpdatetime())) {
            queryWrapper = queryWrapper.like("case_updatetime",usecase.getCaseUpdatetime());
         }
        String  sysUser = Utility.getCurrentUsername();
        SysUser account=sysUserMapper.selectByUsername(sysUser);
        model.addAttribute("account", account.getAccount());

        List<Usecase> list  = usecaseService.list();
        Map<Long,String > types= new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            SysUser username = userService.selectByUsername(list.get(i).getEquUser().toString());
            types.put(list.get(i).getId(), username.getAccount());
        }
        model.addAttribute("usernames",types);


        IPage<Usecase> pageInfo = usecaseService.page(page, queryWrapper);
        model.addAttribute("caseCreatetimeSpace", caseCreatetimeSpace);
        model.addAttribute("caseUpdatetimeSpace", caseUpdatetimeSpace);
        model.addAttribute("searchInfo", usecase);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"list";
    }

    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("addBefore")
    public String addBefore(){
        return prefix+"add";
    }
    /**
    * 添加
    * @param usecase
    * @return
    */
    @PostMapping("add")
    @ResponseBody
    public AjaxResult add(Usecase usecase){
        return toAjax(usecaseService.save(usecase));
    }
    /**
    * 添加跳转页面
    * @return
    */
    @GetMapping("editBefore/{id}")
    public String editBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("usecase",usecaseService.getById(id));
        return prefix+"edit";
    }

    @GetMapping("editsBefore/{id}")
    public String editsBefore(Model model,@PathVariable("id")Long id){
        model.addAttribute("usecase",usecaseService.getById(id));
        return prefix+"edits";
    }

    /**
    * 添加
    * @param usecase
    * @return
    */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(Usecase usecase){
        return toAjax(usecaseService.updateById(usecase));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(usecaseService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    @ResponseBody
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(usecaseService.removeByIds(ids));
    }


    /**
     * 个人信息展示
     * @param model
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping("showInfo")
    public String showInfo(Model model,
                           @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                           @RequestParam(value = "name", required = false, defaultValue = "") String name) {
        SysUser user = new SysUser();
        String loginUserName = Utility.getCurrentUsername();
        if (name != null && !"".equals(name.trim())) {
            user.setUsername(name);
            model.addAttribute("name", name.trim());
        }

        Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
        IPage<SysUser> pageInfo = userService.selectByUserForPage(page, loginUserName);

        model.addAttribute("pageInfo", new PageInfo<SysUser>(pageInfo));
        return prefix + "show/showInfo";
    }

    /**
     * 个人信息编辑页面
     * @param model
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @RequestMapping("showInfoEdit")
    public String showInfoEdit(Model model,
                               @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                               @RequestParam(value = "name", required = false, defaultValue = "") String name) {
        SysUser user = new SysUser();
        String loginUserName = Utility.getCurrentUsername();
        if (name != null && !"".equals(name.trim())) {
            user.setUsername(name);
            model.addAttribute("name", name.trim());
        }

        Page<SysUser> page = new Page<SysUser>(pageNo, pageSize);
        IPage<SysUser> pageInfo = userService.selectByUserForPage(page, loginUserName);

        model.addAttribute("pageInfo", new PageInfo<SysUser>(pageInfo));
        return prefix + "show/showInfoEdit";
    }




    /**
     * 编辑个人信息跳转页面
     * @return
     */
    @GetMapping("showEdit/{id}")
    public String showEdit(Model model,@PathVariable("id")Long id){
        model.addAttribute("user",userService.selectByPrimaryKey(id));
        return prefix+"show/showEdit";
    }


    /**
     * 编辑
     * @param user
     * @return
     */
    @PostMapping("editToShow")
    @ResponseBody
    public AjaxResult editToShow(SysUser user){


        user.setPassword(Utility.QuickPassword(user.getPassword()));
        user.setCreatetime(new Date());
        user.setUpdatetime(new Date());

        return AjaxResult.success(userService.updateByPrimaryKeySelective(user));
    }

    /**
     * 设备发放
     *
     * @param model
     * @param pageNo
     * @param pageSize
     * @param usecase
     * @return
     */
    @RequestMapping("returnList")
    public String returnList(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String caseCreatetimeSpace, String caseUpdatetimeSpace, Usecase usecase) {
        Page<Usecase> page = new Page<Usecase>(pageNo, pageSize);
        QueryWrapper<Usecase> queryWrapper = new QueryWrapper<Usecase>();
        if(!ObjectUtils.isEmpty(usecase.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",usecase.getEquId());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",usecase.getEquName());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",usecase.getEquType());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquUser())) {
            queryWrapper = queryWrapper.like("equ_user",usecase.getEquUser());
        }

        String user = Utility.getCurrentUsername();
         SysUser users =  userService.selectByUsername(user);
        model.addAttribute("username", users.getAccount());
        queryWrapper.eq("equ_status", "已归还");
        queryWrapper.eq("equ_staff",users.getId());

        List<Usecase> list  = usecaseService.list();
        Map<Long,String > types= new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            SysUser username = userService.selectByUsername(list.get(i).getEquUser().toString());
            types.put(list.get(i).getId(), username.getAccount());
        }
        model.addAttribute("usernames",types);

        IPage<Usecase> pageInfo = usecaseService.page(page, queryWrapper);
        model.addAttribute("caseCreatetimeSpace", caseCreatetimeSpace);
        model.addAttribute("caseUpdatetimeSpace", caseUpdatetimeSpace);
        model.addAttribute("searchInfo", usecase);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"List/returnList";
    }


    /**
     * 设备归还
     */
    @RequestMapping("grantList")
    public String grantList(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String caseCreatetimeSpace, String caseUpdatetimeSpace, Usecase usecase) {
        Page<Usecase> page = new Page<Usecase>(pageNo, pageSize);
        QueryWrapper<Usecase> queryWrapper = new QueryWrapper<Usecase>();
        if(!ObjectUtils.isEmpty(usecase.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",usecase.getEquId());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",usecase.getEquName());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",usecase.getEquType());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquUser())) {
            queryWrapper = queryWrapper.like("equ_user",usecase.getEquUser());
        }

        String user = Utility.getCurrentUsername();
        SysUser users =  userService.selectByUsername(user);
        model.addAttribute("username", users.getAccount());
        queryWrapper.eq("equ_status", "已发放");
        queryWrapper.eq("equ_staff",users.getId());

        List<Usecase> list  = usecaseService.list();
        Map<Long,String > types= new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            SysUser username = userService.selectByUsername(list.get(i).getEquUser().toString());
            types.put(list.get(i).getId(), username.getAccount());
        }
        model.addAttribute("usernames",types);

        IPage<Usecase> pageInfo = usecaseService.page(page, queryWrapper);
        model.addAttribute("caseCreatetimeSpace", caseCreatetimeSpace);
        model.addAttribute("caseUpdatetimeSpace", caseUpdatetimeSpace);
        model.addAttribute("searchInfo", usecase);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"List/grantList";
    }

    @RequestMapping("only")
    public String only(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String caseCreatetimeSpace, String caseUpdatetimeSpace, Usecase usecase) {
        Page<Usecase> page = new Page<Usecase>(pageNo, pageSize);
        QueryWrapper<Usecase> queryWrapper = new QueryWrapper<Usecase>();
        if(!ObjectUtils.isEmpty(usecase.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",usecase.getEquId());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",usecase.getEquName());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",usecase.getEquType());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquUser())) {
            queryWrapper = queryWrapper.like("equ_user",usecase.getEquUser());
        }

        String user = Utility.getCurrentUsername();
        SysUser users =  userService.selectByUsername(user);
        model.addAttribute("username", users.getAccount());
        queryWrapper.eq("equ_user",users.getUsername());
        queryWrapper.eq("equ_status", "已发放");

        List<Usecase> list  = usecaseService.list();
        Map<Long,String > types= new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            SysUser username = userService.selectByPrimaryKey(list.get(i).getEquStaff());
            types.put(list.get(i).getId(), username.getAccount());
        }
        model.addAttribute("account",types);

        IPage<Usecase> pageInfo = usecaseService.page(page, queryWrapper);
        model.addAttribute("caseCreatetimeSpace", caseCreatetimeSpace);
        model.addAttribute("caseUpdatetimeSpace", caseUpdatetimeSpace);
        model.addAttribute("searchInfo", usecase);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"only/list";
    }


    @RequestMapping("already")
    public String already(Model model,@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String caseCreatetimeSpace, String caseUpdatetimeSpace, Usecase usecase) {
        Page<Usecase> page = new Page<Usecase>(pageNo, pageSize);
        QueryWrapper<Usecase> queryWrapper = new QueryWrapper<Usecase>();
        if(!ObjectUtils.isEmpty(usecase.getEquId())) {
            queryWrapper = queryWrapper.like("equ_id",usecase.getEquId());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquName())) {
            queryWrapper = queryWrapper.like("equ_name",usecase.getEquName());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquType())) {
            queryWrapper = queryWrapper.like("equ_type",usecase.getEquType());
        }
        if(!ObjectUtils.isEmpty(usecase.getEquUser())) {
            queryWrapper = queryWrapper.like("equ_user",usecase.getEquUser());
        }

        String user = Utility.getCurrentUsername();
        SysUser users =  userService.selectByUsername(user);
        model.addAttribute("username", users.getAccount());
        queryWrapper.eq("equ_user",users.getUsername());
        queryWrapper.eq("equ_status", "已归还");

        List<Usecase> list  = usecaseService.list();
        Map<Long,String > types= new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            SysUser username = userService.selectByPrimaryKey(list.get(i).getEquStaff());
            types.put(list.get(i).getId(), username.getAccount());
        }
        model.addAttribute("account",types);

        IPage<Usecase> pageInfo = usecaseService.page(page, queryWrapper);
        model.addAttribute("caseCreatetimeSpace", caseCreatetimeSpace);
        model.addAttribute("caseUpdatetimeSpace", caseUpdatetimeSpace);
        model.addAttribute("searchInfo", usecase);
        model.addAttribute("pageInfo", new PageInfo(pageInfo));
        return prefix+"only/already";
    }


}

